package com.baizhi.controller;


import com.baizhi.constants.RedisPrefix;
import com.baizhi.dto.VideoFeedDTO;
import com.baizhi.entity.User;
import com.baizhi.feign.VideoFeign;
import com.baizhi.po.FeedPO;
import com.baizhi.service.FeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 动态(Feed)表控制层
 *
 * @author makejava
 * @since 2021-09-27 09:35:40
 */
@RefreshScope
@RestController
@RequestMapping
public class FeedController {
    /**
     * 服务对象
     */
    @Resource
    private FeedService feedService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VideoFeign videoFeign;

    @GetMapping("/feeds")
    public List<FeedPO> queryAll(@RequestParam("token") String token, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(required = false, defaultValue = "10", value = "per_page") Integer per_page) {
        /**
         * 有机会这样写，先展示全部的
         * TODO: 获取到当前用户，根据当前用户去找到对应的关注表，，获取到全部的该用户关注的人的id，再去动态表中获取这几个人的动态+自己的动态，根据时间进行倒叙排序
         *
         */
        /*User user = (User) redisTemplate.opsForValue().get(RedisPrefix.USER_TOKEN_PREFIX + token);
        Integer userId = user.getId();
        //被关注的人
        List<Integer> integers = videoFeign.queryAllFollowingByUserId(userId);*/

        //动态表中获取数据，获取按时间排序再根据动态表获取数据
        List<FeedPO> feedPOS = feedService.queryAllFeed(page, per_page);
        return feedPOS;
    }

    /**
     * 发布动态
     *
     * @param file
     * @param content
     */
    @PostMapping("/user/feeds")
    public void insert(List<MultipartFile> file, String content, @RequestParam("token") String token) {
        /**
         * TODO : 给视频添加加入动态中
         */
        //根据token获取当前用户id
        User user = (User) redisTemplate.opsForValue().get(RedisPrefix.USER_TOKEN_PREFIX + token);
        if (user != null) {
            //调用已登录用户信息
            feedService.insert(file, content, user.getId());
        } else {
            throw new RuntimeException("请登录后再进行操作");
        }

    }

    /**
     * 处理视频上传至动态
     *
     * @param videoFeed
     */
    @PostMapping("/user/feeds/video")
    public void videoFeed(VideoFeedDTO videoFeed) {
        feedService.insertVideo(videoFeed);
    }
}

